"""
**************************
Author:Jane
Time:2021/6/23 23:40
Location:杭州
Month:六月
Email:393162576@qq.com
**************************
"""
import pymysql
from common.handle_config import conf


class DB:
    def __init__(self):
        # 创建一个连接对象
        self.conn = pymysql.connect(host=conf.get("mysql","host"),
                             port=conf.getint("mysql","port"),
                             username=conf.get("mysql","user"),
                             password=conf.get("mysql","password"),
                             charset=conf.get("mysql","charset"),
                             cursorclass=pymysql.cursors.Dictcursor
                             )
        # 创建一个游标
        self.cur = self.conn.cursor()

    def find_one(self,sql):
        """获取查询出来的第一条数据"""
        # 执行查询语句,先提交事务
        self.conn.commit()
        self.cur.execute(sql)
        data = self.cur.fetchone()
        return data

    def find_all(self,sql):
        """获取查询出来的所有数据"""
        self.conn.commit()
        self.cur.execute(sql)
        data = self.cur.fetchall()
        return data
    def find_count(self,sql):
        """返回查询数据的条数"""
        self.conn.commit()
        return self.cur.execute(sql)

    def close(self):
        """关闭游标，断开连接"""
        self.cur.close()
        self.conn.close()

